import AntModal from "@/components/pt/dialog/AntModal";
import UserList from "@/views/biz/customer/modules/components/userlist";
import ChangeUser from "@/views/system/user/modules/ChangeUser";
import {
  getBizJinjianInfo,
  addBizJinjianInfo,
  updateBizJinjianInfo,
  findMaxSort,
} from "@/api/biz/bizJinjianInfo";
import { listBizProductInfo } from "@/api/biz/bizProductInfo";

export default {
  name: "CreateForm",
  props: {
    jinjianTypeOptions: {
      type: Array,
      required: true,
    },

    backTypeOptions: {
      type: Array,
      required: true,
    },
    orgTypeOptions: {
      type: Array,
      required: true,
    },
    customerInfo: {
      type: Object,
      required: true,
    },
    jinjianStatus: {
      type: Number,
    },
  },
  components: {
    AntModal,
    UserList,
    ChangeUser,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加客户进件",
      // 表单参数
      form: {},
      rules: {
        servcieUserName: [
          { required: true, message: "客服不能为空", trigger: "blur" },
        ],
        yxEdu: [
          { required: true, message: "申请金额不能为空", trigger: "blur" },
        ],
        sjEdu: [
          { required: true, message: "签约金额不能为空", trigger: "blur" },
        ],
        serviceAmount: [
          { required: true, message: "服务费用不能为空", trigger: "blur" },
        ],
      }, //
      productList: [],
      visibleShowUser: false,
      chooseUserVisible: false,
      proTwo: false,
      proThree: false,
      isFanganSave: false,
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    // 获取选择后的人员信息
    handleManualShare(users) {
      console.log(users);
      this.form.servcieUserId = users[0].id;
      this.form.servcieUserName = users[0].name;
      let userIds = "";
      let userName = "";
      if (users.length > 1) {
        // users.map((item) => {
        //   userIds += item.id + ",";
        //   userName += item.name + ",";
        // });

        for (var i = 0; i < users.length; i++) {
          var item = users[i];
          if (i === users.length - 1) {
            userIds += item.id;
            userName += item.name;
          } else {
            userIds += item.id + ",";
            userName += item.name + ",";
          }
        }
        this.form.servcieUserId = userIds;
        this.form.servcieUserName = userName;
      }
    },
    handleChooseProduct(val, options) {
      this.form.productId = val;
      this.form.productName = options.key;
      console.log(val, options);
    },
    handleChooseProductTwo(val, options) {
      this.form.productIdTwo = val;
      this.form.productNameTwo = options.key;
      console.log(val, options);
    },
    handleChooseProductThree(val, options) {
      this.form.productIdThree = val;
      this.form.productNameThree = options.key;
      console.log(val, options);
    },
    handleOnSearchCustomer() {
      this.visibleShowUser = true;
    },
    // 获取客户信息
    getCustomerInfo(item) {
      // this.customerInfo = item;
      this.form.customerName = item.cusName;
      if (item.id) {
        this.form.customerId = item.id;
      } else {
        this.form.customerId = item.customerId;
      }
      // this.form.customerId = item.customerId;
      this.form.phone = item.phone;
      this.form.idCard = item.idCard;
      this.form.tuHomeUserId = item.toHomeUserId; // 如果是上门进行方案确认的
      this.visibleShowUser = false;
      console.log(item);
    },
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        monthAmount: undefined,

        companyId: undefined,

        customerId: undefined,

        idCard: undefined,

        customerName: undefined,

        phone: undefined,

        orgType: undefined,

        productId: undefined,

        productName: undefined,

        jinjianType: undefined,

        yxEdu: undefined,

        sjEdu: undefined,

        lv: undefined,

        backType: undefined,

        fenqiTime: undefined,

        diyaTime: undefined,

        productTiaojian: undefined,

        qiandanTime: undefined,

        serviceAmount: undefined,

        serviceAmountRemark: undefined,

        servcieUserName: undefined,

        servcieUserId: undefined,

        sort: undefined,

        status: undefined,
      };
    },
    // 预审
    handleYuShen(status) {
      this.form.status = status;
      if (status === "1") {
        this.form.jinjianStatus = "2";
      }
      updateBizJinjianInfo(this.form).then((res) => {
        this.$message.success("操作成功", 3);
        this.open = false;
        this.$emit("ok");
        this.$emit("close");
      });
    },
    // 审批通过
    handleJinjianShenhe(status) {
      this.form.jinjianStatus = status;
      if (status === "8") {
        this.form.isGps = "1";
      }
      updateBizJinjianInfo(this.form).then((res) => {
        this.$message.success("操作成功", 3);
        this.open = false;
        this.$emit("ok");
        this.$emit("close");
      });
    },
    /** 获取产品信息 */
    handleGetProductInfo(value) {
      listBizProductInfo({ orgType: value, pageSize: 50 }).then((res) => {
        this.productList = res.data.list;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加客户进件";
      });
      this.getCustomerInfo(this.customerInfo);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizJinjianInfoId = row.id;
      getBizJinjianInfo(bizJinjianInfoId).then((response) => {
        this.form = response.data;
        this.formTitle = "更新客户进件";
        this.spinning = !this.spinning;
        this.handleGetProductInfo(this.form.orgType);
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.isFanganSave = true;
      this.$refs.form.validate((valid) => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form));
          saveForm.serviceAmount =
            saveForm.zfei + (saveForm.yxEdu * saveForm.glFei) / 100;
          if (this.form.id !== undefined) {
            updateBizJinjianInfo(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
              this.isFanganSave = false;
            });
          } else {
            if (this.jinjianStatus === 2) {
              saveForm.jinjianStatus = this.jinjianStatus;
              saveForm.status = "1";
            } else {
              saveForm.status = "0";
              saveForm.jinjianStatus = "0";
            }
            // saveForm.customerId = this.customerInfo.id;
            addBizJinjianInfo(saveForm).then((response) => {
              this.$message.success("保存成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
              this.isFanganSave = false;
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/jinjianinfo/index";
      this.$router.push(index);
    },
  },
};
